package com.tiantian.quanminxiu.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.Window;
import android.view.WindowManager;

import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.tiantian.quanminxiu.R;
import com.tiantian.quanminxiu.module.Module_Header;
import com.umeng.analytics.MobclickAgent;

import cn.pedant.SweetAlert.SweetAlertDialog;

/*
* Fragment的基类
* */
public abstract class ActivityBase extends android.support.v7.app.AppCompatActivity {

    //region 成员属性
    protected Intent intent;
    protected Context thisContext;
    protected Activity thisActivity;
    protected ApplicationBase thisApplication;
    protected SwipeRefreshLayout mSwipeRefresh = null;
    protected Module_Header mHeader = null;
    //endregion

    //region 初始化视图 Init()
    protected void Init(boolean isTransStatus) {
        thisActivity = this;
        thisContext = this;
        thisApplication = (ApplicationBase) thisContext.getApplicationContext();
        mHeader = (Module_Header) findViewById(R.id.mHeader);
        mSwipeRefresh = (SwipeRefreshLayout) findViewById(R.id.mSwipeRefresh);
        if (mSwipeRefresh != null) {
            mSwipeRefresh.setColorSchemeResources(android.R.color.holo_red_light
                    , android.R.color.holo_red_light
                    , android.R.color.holo_orange_light
                    , android.R.color.holo_green_light);
        }
        thisApplication.addActivity(thisActivity);

        if (!isTransStatus){
            return;
        }

        //沉浸式状态栏设置
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
            SystemBarTintManager tintManager = new SystemBarTintManager(this);
            tintManager.setStatusBarTintEnabled(true);
            tintManager.setStatusBarTintResource(R.color.float_transparent);
        }
    }
    //endregion

    public void Init(){
        Init(true);
    }

    //region 初始化加载界面 PageInit
    public abstract void InitView();
    //endregion

    //region 初始化加载界面 PageInit
    public abstract void PageInit();
    //endregion

    //region 沉浸式状态栏 setTranslucentStatus
    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }
    //endregion

    protected SweetAlertDialog mSweetDialog = null;

    //region 是否显示SweetAlertDialog  SweetDialog_Show()
    protected void SweetDialog_Show() {
        if (mSweetDialog != null && !mSweetDialog.isShowing()) {
            mSweetDialog.show();
        }
    }
    //endregion

    //region 返回控件自带的样式的SweetAlertDialog SweetDialog_Init(int alertType)
    protected SweetAlertDialog SweetDialog_Init(int alertType) {
        if (mSweetDialog == null || !mSweetDialog.isShowing()) {
            mSweetDialog = new SweetAlertDialog(thisActivity);
        }
        mSweetDialog.setOnDismissListener(null);
        mSweetDialog.setOnCancelListener(null);
        mSweetDialog.setOnKeyListener(null);
        mSweetDialog.setOnShowListener(null);
        mSweetDialog.setCanceledOnTouchOutside(true);
        mSweetDialog.setCancelable(true);
        switch (alertType) {
            case SweetAlertDialog.SUCCESS_TYPE:
            case SweetAlertDialog.ERROR_TYPE:
                mSweetDialog.showCancelButton(false)
                        .setConfirmText("确定")
                        .setConfirmClickListener(null)
                        .changeAlertType(alertType);
                break;
            case SweetAlertDialog.PROGRESS_TYPE:
                mSweetDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
                mSweetDialog.setTitleText("Loading")
                        .showCancelButton(false)
                        .showContentText(false)
                        .changeAlertType(SweetAlertDialog.PROGRESS_TYPE);
                mSweetDialog.setCanceledOnTouchOutside(false);
                break;
            case SweetAlertDialog.WARNING_TYPE:
                mSweetDialog.showCancelButton(true)
                        .setCancelClickListener(null)
                        .setConfirmClickListener(null)
                        .changeAlertType(SweetAlertDialog.WARNING_TYPE);
                break;
            case SweetAlertDialog.NORMAL_TYPE:
                mSweetDialog.showCancelButton(false)
                        .setConfirmText("确定")
                        .setConfirmClickListener(null)
                        .changeAlertType(SweetAlertDialog.WARNING_TYPE);
                break;
        }
        return mSweetDialog;
    }
    //endregion

    //region 返回网络异常SweetAlertDialog SweetDialog_InitNetworkTips(String titleText, String contentText)
    protected SweetAlertDialog SweetDialog_InitNetworkTips(String titleText, String contentText) {
        SweetDialog_Init(SweetAlertDialog.WARNING_TYPE)
                .setTitleText(titleText == null ? "网络错误，请检查网络后重试" : titleText)
                .setContentText(contentText)
                .showContentText(contentText != null)
                .setCancelText("取消返回")
                .setConfirmText("确定重试")
                .setCancelClickListener(sDialog -> finish())
                .setCancelable(false);
        return mSweetDialog;
    }
    //endregion

    //region 返回SweetAlertDialog SweetDialog_InitNetworkTips(String contentText)
    //调用SweetDialog_InitNetworkTips(String titleText, String contentText)
    protected SweetAlertDialog SweetDialog_InitNetworkTips(String contentText) {
        return SweetDialog_InitNetworkTips(null, contentText);
    }
    //endregion

    //region 返回SweetAlertDialog的函数 SweetDialog_InitNetworkTips()
    //调用SweetDialog_InitNetworkTips(String titleText, String contentText)
    protected SweetAlertDialog SweetDialog_InitNetworkTips() {
        return SweetDialog_InitNetworkTips(null, null);
    }
    //endregion

    //region 定义的加载样式type final   static修饰
    protected final static byte LOADING_TYPE_DEFAULT = 1;
    protected final static byte LOADING_TYPE_TOP = 2;
    protected final static byte LOADING_TYPE_DIALOG = 3;
    //endregion

    //region 加载样式 Loading_Open(byte LOADING_TYPE, boolean mark)
    protected void Loading_Open(byte LOADING_TYPE, boolean mark) {
        if (LOADING_TYPE == LOADING_TYPE_TOP && mSwipeRefresh == null) {
            LOADING_TYPE = LOADING_TYPE_DEFAULT;
        }
        if (LOADING_TYPE == LOADING_TYPE_DEFAULT && mHeader == null) {
            LOADING_TYPE = LOADING_TYPE_DIALOG;
        }
        if (mSwipeRefresh != null
                && mSwipeRefresh.isRefreshing()
                && (LOADING_TYPE == LOADING_TYPE_TOP || LOADING_TYPE == LOADING_TYPE_DEFAULT)) {
            return;
        }
        if (mSweetDialog != null && mSweetDialog.isShowing()) {
            LOADING_TYPE = LOADING_TYPE_DIALOG;
        }
        switch (LOADING_TYPE) {
            case LOADING_TYPE_TOP:
                mSwipeRefresh.post(() -> mSwipeRefresh.setRefreshing(true));
                break;
            case LOADING_TYPE_DIALOG:
                SweetDialog_Init(SweetAlertDialog.PROGRESS_TYPE)
                        .setCancelable(!mark);
                SweetDialog_Show();
                break;
            case LOADING_TYPE_DEFAULT:
                mHeader.Loading_Show(mark);
                break;
        }
    }
    //endregion

    //region 加载样式Loading_Open(byte LOADING_TYPE),调用Loading_Open(LOADING_TYPE, false)
    public void Loading_Open(byte LOADING_TYPE) {
        Loading_Open(LOADING_TYPE, false);
    }
    //endregion

    //region 加载样式Loading_Open(boolean mark),调用Loading_Open(LOADING_TYPE_DEFAULT, mark)
    public void Loading_Open(boolean mark) {
        Loading_Open(LOADING_TYPE_DEFAULT, mark);
    }
    //endregion

    //region 加载样式Loading_Open(),调用Loading_Open(LOADING_TYPE_DEFAULT, mark)
    public void Loading_Open() {
        Loading_Open(LOADING_TYPE_DEFAULT, false);
    }
    //endregion

    //region 关闭显示加载的AlertDialog Loading_Close()
    public void Loading_Close() {
        if (mSweetDialog != null && mSweetDialog.isShowing() && mSweetDialog.getAlerType() == SweetAlertDialog.PROGRESS_TYPE) {
            mSweetDialog.dismissWithAnimation();
        }
        if (mHeader != null) {
            mHeader.Loading_Hide();
        }
        if (mSwipeRefresh != null) {
            mSwipeRefresh.setRefreshing(false);
        }
    }
    //endregion

    //region mvpView中重写 getContext
    public Context getContext(){
        return thisContext;
    }
    //endregion

    //region 当前控件恢复运行的回调 onResume（）
    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }
    //endregion

    //region onPause（）回调
    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
    //endregion

    //region onDestroy
    @Override
    protected void onDestroy() {
        super.onDestroy();
        thisApplication.removeActivity(thisActivity);
    }
    //endregion
}